package mappers;

import handler.DatabaseConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Event;
import model.Match;
import model.Player;

public class EventMapper {

    private static EventMapper instance;

    private EventMapper() {

    }

    //Henter alle events for en kamp og laver dem til objekter
    public ArrayList<Event> getEvents(Match selectedMatch) {

        ArrayList<Event> events = new ArrayList<>();
        try {
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();
            
            String SQL = "Select * from occasion where gameid = " + selectedMatch.getMatchId();
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                int id = rs.getInt("eventid");
                String type = rs.getString("eventtype");
                String moment = rs.getString("moment");
                int gameId = rs.getInt("gameid");
                int playerId = rs.getInt("playerid");
                Player player = PlayerMapper.getInstance().getPlayer(playerId);
                Match match = MatchMapper.getInstance().getMatch(gameId);

                events.add(new Event(id, type, moment, match, player));

            }
            
            stmt.close();
            rs.close();
        } catch (SQLException ex) {
            System.out.println("SQL Fejl: " + ex.getMessage());

        }

        return events;

    }

    // Man skal kunne lagre hver event.
    public void createEvent(int id, String type, int moment, int playerid, int matchid) {
        try {
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();

            String SQL = "insert into occasion() values (";
            SQL += id + ",'" + type + "'," + moment + "," + matchid + "," + playerid + ")";

            stmt.execute(SQL);
            stmt.close();
        } catch (SQLException ex) {
            System.out.println("SQL FEJL: " + ex.getMessage());
        }

    }

    //Tæller antallet af event-rækker
    public int getCount() {
        int count = 0;
        try {
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();

            String SQL = "select count(eventId) as eventId from occasion";
            ResultSet rs = stmt.executeQuery(SQL);

            rs.next();

            count = rs.getInt("eventId");

            stmt.close();
            rs.close();
            
        } catch (SQLException ex) {
            System.out.println("SQL FEJL: " + ex.getMessage());

        }
        return count;
    }

    public static EventMapper getInstance() {
        if (instance == null) {
            instance = new EventMapper();
        }
        return instance;
    }
    
    //Henter alle scoringer og selvmål for en bestemt kamp.
    public ArrayList<Event> getMatchEvents(Match selectedMatch) {

        ArrayList<Event> events = new ArrayList<>();
        try {
            Statement stmt = DatabaseConnection.getDbInstance().getConnection().createStatement();
            
            String SQL = "Select * from occasion where gameid = " + selectedMatch.getMatchId();
            SQL += " and eventtype = 'Scoring' or eventtype = 'Selvmål'";
            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                int id = rs.getInt("eventid");
                String type = rs.getString("eventtype");
                String moment = rs.getString("moment");
                int gameId = rs.getInt("gameid");
                int playerId = rs.getInt("playerid");
                Player player = PlayerMapper.getInstance().getPlayer(playerId);
                Match match = MatchMapper.getInstance().getMatch(gameId);

                events.add(new Event(id, type, moment, match, player));
            }
            
            stmt.close();
            rs.close();
        } catch (SQLException ex) {
            System.out.println("SQL Fejl: " + ex.getMessage());

        }

        return events;

    }

}
